package com.download.zhuaba.Service;

import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.net.MalformedURLException;

public class WelcomeService {
    public static String getHtml(){
        WebClient webClient = new WebClient(BrowserVersion.FIREFOX_52);//模拟火狐浏览器
        try {

            webClient.setJavaScriptTimeout(5000);
            webClient.getOptions().setUseInsecureSSL(true);//接受任何主机连接 无论是否有有效证书
            webClient.getOptions().setJavaScriptEnabled(true);//设置支持javascript脚本
            webClient.getOptions().setCssEnabled(false);//禁用css支持

            webClient.getOptions().setThrowExceptionOnScriptError(false);//js运行错误时不抛出异常
            webClient.getOptions().setTimeout(100000);//设置连接超时时间
            webClient.getOptions().setDoNotTrackEnabled(false);

            HtmlPage page = webClient.getPage("https://baike.baidu.com/calendar/");

            Thread.sleep(3000);
            String xml = page.asXml();
            Document document = Jsoup.parse(xml);//获取html文档
            Elements elements = document.getElementsByClass("events-container");

            return elements.html();
        } catch (FailingHttpStatusCodeException e) {
            // TODO 自动生成的 catch 块
            e.printStackTrace();
        } catch (MalformedURLException e) {
            // TODO 自动生成的 catch 块
            e.printStackTrace();
        } catch (IOException e) {
            // TODO 自动生成的 catch 块
            e.printStackTrace();
        } catch (InterruptedException e) {
            // TODO 自动生成的 catch 块
            e.printStackTrace();
        }finally {
            webClient.close();
        }
        return null;
    }
}
